<template>
	<view class="news-detail-page">
		<!-- 导航栏 -->
		<view class="nav-bar">
			<view class="nav-back" @click="goBack">
				<uni-icons type="arrowleft" size="20" color="#333"></uni-icons>
			</view>
			<text class="nav-title">新闻详情</text>
			<view class="nav-actions">
				<uni-icons type="more" size="20" color="#333"></uni-icons>
			</view>
		</view>

		<scroll-view scroll-y="true" class="content-scroll">
			<!-- 新闻标题区域 -->
			<view class="news-header">
				<view class="news-title-section">
					<text class="news-title">{{ newsData.title }}</text>
					<view class="news-source-info">
						<text class="news-source">{{ newsData.source }}</text>
						<text class="news-time">12/14 10:53</text>
					</view>
				</view>

				<view class="author-actions">
					<button class="follow-btn" @click="toggleFollow">
						<text v-if="!isFollowed">+ 关注</text>
						<text v-else>已关注</text>
					</button>
				</view>
			</view>

			<!-- 新闻内容 -->
			<view class="news-content">
				<text class="content-text">{{ newsData.content }}</text>
				<image v-if="newsData.hasImage" :src="getImageUrl(newsData.imageUrl)" class="content-image"
					mode="widthFill" @error="handleContentImageError"></image>
			</view>

			<!-- 精彩推荐 -->
			<view class="recommend-section">
				<text class="section-title">精彩推荐</text>
				<view class="recommend-list">
					<view v-for="(item, index) in recommendedNews" :key="index" class="recommend-item"
						@click="goToRecommend(item)">
						<!-- 添加图片展示 -->
						<view class="recommend-image-container" v-if="item.imageUrl">
							<image :src="getImageUrl(item.imageUrl)" class="recommend-image" mode="aspectFill"
								:alt="item.title" @error="handleImageError(index, $event)"></image>
							<view class="image-badge" v-if="item.type">{{ getTypeText(item.type) }}</view>
						</view>

						<view class="recommend-content">
							<text class="recommend-title">{{ item.title }}</text>
							<text class="recommend-meta">{{ item.source }} · {{ item.comments }}评</text>
						</view>
					</view>
				</view>
			</view>

			<!-- 评论区域 -->
			<view class="comments-section">
				<view class="comments-header">
					<text class="section-title">评论</text>
					<text class="comments-count">{{ newsData.comments }}条评论</text>
				</view>

				<!-- 评论输入框 -->
				<view class="comment-input-area">
					<view class="input-box">
						<input v-model="newComment" placeholder="我来说两句..." class="comment-input"
							@confirm="addComment" />
						<button class="send-btn" @click="addComment">发送</button>
					</view>
				</view>

				<!-- 评论列表 -->
				<view class="comments-list">
					<view v-for="(comment, index) in commentsList" :key="index" class="comment-item">
						<view class="comment-header">
							<image :src="comment.avatar" class="comment-avatar" @error="handleAvatarError(index)">
							</image>
							<view class="comment-user">
								<text class="user-name">{{ comment.userName }}</text>
								<text class="comment-time">{{ comment.time }}</text>
							</view>
						</view>
						<text class="comment-content">{{ comment.content }}</text>
						<view class="comment-actions">
							<view class="like-action" @click="toggleLikeComment(index)">
								<uni-icons :type="comment.isLiked ? 'heart-filled' : 'heart'" size="14"
									:color="comment.isLiked ? '#f00' : '#666'"></uni-icons>
								<text class="like-count">{{ comment.likes }}</text>
							</view>
							<text class="reply-action">回复</text>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 新闻数据
				newsData: {},

				// 关注状态
				isFollowed: false,

				// 评论相关
				newComment: '',
				commentsList: [{
						userName: '网友8725037',
						avatar: '/static/avatars/user1.jpg',
						content: '微笑生活，勇敢面对。',
						time: '2小时前',
						likes: 5,
						isLiked: false,
						location: '广西盐城市'
					},
					{
						userName: '平安379373',
						avatar: '/static/avatars/user2.jpg',
						content: '真看不懂，理解不了。',
						time: '3小时前',
						likes: 2,
						isLiked: true,
						location: '广西百色市'
					}
				],


				recommendedNews: [{
						title: "单节24分，5个三分球!库里打疯了，半场狂胜25分",
						source: "科技高分",
						comments: 3737,
						imageUrl: "city1.png", // 使用city1图片
						type: "sports",
					},
					{
						title: "意甲第35轮国米进攻的弱点是什么，国米进攻优势是什么",
						source: "科技高分",
						comments: 9677,
						imageUrl: "city2.jpg", // 使用city2图片
						type: "sports",
					}
				]
			}
		},

		onLoad(options) {
			if (options.newsData) {
				this.newsData = JSON.parse(decodeURIComponent(options.newsData));
				console.log('新闻详情数据:', this.newsData);
			}

			// 调试图片路径
			console.log('推荐新闻图片路径:');
			this.recommendedNews.forEach((item, index) => {
				console.log(`新闻${index + 1}:`, this.getImageUrl(item.imageUrl));
			});
		},

		methods: {
			// 返回上一页
			goBack() {
				uni.navigateBack();
			},

			// 切换关注状态
			toggleFollow() {
				this.isFollowed = !this.isFollowed;
				uni.showToast({
					title: this.isFollowed ? '关注成功' : '已取消关注',
					icon: 'success'
				});
			},

			// 添加评论
			addComment() {
				if (!this.newComment.trim()) {
					uni.showToast({
						title: '请输入评论内容',
						icon: 'none'
					});
					return;
				}

				const newComment = {
					userName: '当前用户',
					avatar: '/static/avatars/current-user.jpg',
					content: this.newComment,
					time: '刚刚',
					likes: 0,
					isLiked: false,
					location: '本地'
				};

				this.commentsList.unshift(newComment);
				this.newComment = '';

				// 更新评论数量
				this.newsData.comments = (this.newsData.comments || 0) + 1;

				uni.showToast({
					title: '评论成功',
					icon: 'success'
				});
			},

			// 点赞评论
			toggleLikeComment(index) {
				const comment = this.commentsList[index];
				comment.isLiked = !comment.isLiked;
				comment.likes += comment.isLiked ? 1 : -1;

				// 更新数据
				this.$set(this.commentsList, index, {
					...comment
				});
			},

			// 跳转到推荐新闻
			goToRecommend(item) {
				uni.showToast({
					title: `跳转到: ${item.title}`,
					icon: 'none'
				});
			},

			// 获取图片URL
			getImageUrl(filename) {



				// 默认图片路径
				return `/static/${filename}`;
			},







			// 获取分类文本
			getTypeText(type) {
				const typeMap = {
					'sports': '体育',
					'tech': '科技',
					'finance': '财经'
				};
				return typeMap[type] || '推荐';
			}
		}
	}
</script>

<style scoped>
	.news-detail-page {
		background-color: #f5f5f5;
		min-height: 100vh;
	}

	/* 导航栏 */
	.nav-bar {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 10px 15px;
		background: #fff;
		border-bottom: 1px solid #f0f0f0;
		position: sticky;
		top: 0;
		z-index: 100;
	}

	.nav-back,
	.nav-actions {
		width: 40px;
		height: 40px;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.nav-title {
		font-size: 16px;
		font-weight: bold;
		color: #333;
	}

	/* 内容区域 */
	.content-scroll {
		height: calc(100vh - 60px);
	}

	/* 新闻标题区域 */
	.news-header {
		background: #fff;
		padding: 20px 15px;
		display: flex;
		justify-content: space-between;
		align-items: flex-start;
		border-bottom: 1px solid #f0f0f0;
	}

	.news-title-section {
		flex: 1;
	}

	.news-title {
		font-size: 20px;
		font-weight: bold;
		line-height: 1.4;
		color: #333;
		display: block;
		margin-bottom: 10px;
	}

	.news-source-info {
		display: flex;
		align-items: center;
	}

	.news-source {
		font-size: 14px;
		color: #666;
		margin-right: 15px;
	}

	.news-time {
		font-size: 12px;
		color: #999;
	}

	.author-actions {
		margin-left: 15px;
	}

	.follow-btn {
		background: #f00;
		color: #fff;
		border: none;
		padding: 6px 12px;
		border-radius: 15px;
		font-size: 12px;
	}

	/* 新闻内容 */
	.news-content {
		background: #fff;
		padding: 20px 15px;
		margin-bottom: 10px;
	}

	.content-text {
		font-size: 16px;
		line-height: 1.6;
		color: #333;
		display: block;
		margin-bottom: 15px;
	}

	.content-image {
		width: 100%;
		border-radius: 8px;
		margin: 15px 0;
	}

	/* 精彩推荐 */
	.recommend-section {
		background: #fff;
		padding: 15px;
		margin-bottom: 10px;
		border-radius: 8px;
		box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
	}

	.section-title {
		font-size: 16px;
		font-weight: bold;
		color: #333;
		display: block;
		margin-bottom: 12px;
		padding-bottom: 8px;
		border-bottom: 1px solid #f0f0f0;
	}

	.recommend-list {
		display: flex;
		flex-direction: column;
		gap: 12px;
	}

	.recommend-item {
		display: flex;
		padding: 10px 0;
		border-bottom: 1px solid #f8f8f8;
		cursor: pointer;
		transition: background-color 0.2s;
	}

	.recommend-item:last-child {
		border-bottom: none;
	}

	.recommend-item:active {
		background-color: #f5f5f5;
	}

	/* 图片容器 */
	.recommend-image-container {
		position: relative;
		flex-shrink: 0;
		width: 80px;
		height: 60px;
		margin-right: 12px;
		border-radius: 4px;
		overflow: hidden;
	}

	.recommend-image {
		width: 100%;
		height: 100%;
		background-color: #f0f0f0;
	}

	/* 图片角标 */
	.image-badge {
		position: absolute;
		top: 4px;
		left: 4px;
		background: rgba(0, 0, 0, 0.7);
		color: white;
		font-size: 10px;
		padding: 2px 6px;
		border-radius: 10px;
	}

	/* 内容区域 */
	.recommend-content {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.recommend-title {
		font-size: 14px;
		color: #333;
		line-height: 1.4;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
		margin-bottom: 4px;
	}

	.recommend-meta {
		font-size: 12px;
		color: #999;
	}

	/* 评论区域 */
	.comments-section {
		background: #fff;
		padding: 15px;
	}

	.comments-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 15px;
	}

	.comments-count {
		font-size: 14px;
		color: #666;
	}

	/* 评论输入 */
	.comment-input-area {
		margin-bottom: 20px;
	}

	.input-box {
		display: flex;
		align-items: center;
		background: #f5f5f5;
		border-radius: 20px;
		padding: 8px 15px;
	}

	.comment-input {
		flex: 1;
		font-size: 14px;
	}

	.send-btn {
		background: none;
		border: none;
		color: #007aff;
		font-size: 14px;
		padding: 5px 10px;
	}

	/* 评论列表 */
	.comment-item {
		padding: 15px 0;
		border-bottom: 1px solid #f0f0f0;
	}

	.comment-item:last-child {
		border-bottom: none;
	}

	.comment-header {
		display: flex;
		align-items: center;
		margin-bottom: 8px;
	}

	.comment-avatar {
		width: 30px;
		height: 30px;
		border-radius: 15px;
		margin-right: 10px;
	}

	.comment-user {
		flex: 1;
	}

	.user-name {
		font-size: 14px;
		color: #333;
		display: block;
	}

	.comment-time {
		font-size: 12px;
		color: #999;
	}

	.comment-content {
		font-size: 14px;
		color: #333;
		line-height: 1.4;
		display: block;
		margin-bottom: 10px;
	}

	.comment-actions {
		display: flex;
		align-items: center;
	}

	.like-action {
		display: flex;
		align-items: center;
		margin-right: 20px;
		font-size: 12px;
		color: #666;
	}

	.like-count {
		margin-left: 5px;
	}

	.reply-action {
		font-size: 12px;
		color: #666;
	}
</style>